package xin.marcher.module.lifecycle.mq.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xin.marcher.module.common.constants.RocketMqConstant;
import xin.marcher.module.lifecycle.mq.config.RocketMQProperties;
import xin.marcher.module.lifecycle.mq.consumer.listener.ItemExpriResultInformListener;
import xin.marcher.module.lifecycle.mq.consumer.listener.ItemUpdateListener;


/**
 *
 */
@Configuration
public class ConsumerBeanConfig {

    /**
     * 配置内容对象
     */
    @Autowired
    private RocketMQProperties rocketMQProperties;

    /**
     * 新增商品消息消费者，负责创建商品生命周期
     *
     * @param itemUpdateListener
     * @return
     * @throws MQClientException
     */
    @Bean("periodStageUpdateTopic")
    public DefaultMQPushConsumer createItemStageConsumer(ItemUpdateListener itemUpdateListener) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(RocketMqConstant.PRODUCT_UPDATE_CONSUMER_GROUP);
        consumer.setNamesrvAddr(rocketMQProperties.getNameServer());
        consumer.subscribe(RocketMqConstant.PRODUCT_UPDATE_TOPIC, "*");
        consumer.registerMessageListener(itemUpdateListener);
        consumer.start();
        return consumer;
    }

    /**
     * 考核结果通知运营消费者
     *
     * @param itemExpriResultInformListener
     * @return
     * @throws MQClientException
     */
    @Bean("itemExpriResultTopic")
    public DefaultMQPushConsumer itemExpriResultInformConsumer(ItemExpriResultInformListener itemExpriResultInformListener) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(RocketMqConstant.ITEM_EXPRI_RESULT_CONSUMER_GROUP);
        consumer.setNamesrvAddr(rocketMQProperties.getNameServer());
        consumer.subscribe(RocketMqConstant.INTERIOR_ITEM_EXPRI_RESULT_TOPIC, "*");
        consumer.registerMessageListener(itemExpriResultInformListener);
        consumer.start();
        return consumer;
    }

}
